package cn.iocoder.yudao.module.ai.controller.admin.picturesort;

import cn.iocoder.yudao.module.ai.common.IdUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.picturesort.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.picturesort.PictureSortDO;
import cn.iocoder.yudao.module.ai.service.picturesort.PictureSortService;

@Tag(name = "管理后台 - 图片分类")
@RestController
@RequestMapping("/ai/picture-sort")
@Validated
public class PictureSortController {

    @Resource
    private PictureSortService pictureSortService;

    @PostMapping("/create")
    @Operation(summary = "创建图片分类")
    @PreAuthorize("@ss.hasPermission('ai:picture-sort:create')")
    public CommonResult<String> createPictureSort(@Valid @RequestBody PictureSortSaveReqVO createReqVO) {
        createReqVO.setId(IdUtils.generateId());
        return success(pictureSortService.createPictureSort(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新图片分类")
    @PreAuthorize("@ss.hasPermission('ai:picture-sort:update')")
    public CommonResult<Boolean> updatePictureSort(@Valid @RequestBody PictureSortSaveReqVO updateReqVO) {
        pictureSortService.updatePictureSort(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除图片分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:picture-sort:delete')")
    public CommonResult<Boolean> deletePictureSort(@RequestParam("id") String id) {
        pictureSortService.deletePictureSort(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得图片分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:picture-sort:query')")
    public CommonResult<PictureSortRespVO> getPictureSort(@RequestParam("id") String id) {
        PictureSortDO pictureSort = pictureSortService.getPictureSort(id);
        return success(BeanUtils.toBean(pictureSort, PictureSortRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得图片分类分页")
    @PreAuthorize("@ss.hasPermission('ai:picture-sort:query')")
    public CommonResult<PageResult<PictureSortRespVO>> getPictureSortPage(@Valid PictureSortPageReqVO pageReqVO) {
        PageResult<PictureSortDO> pageResult = pictureSortService.getPictureSortPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PictureSortRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出图片分类 Excel")
    @PreAuthorize("@ss.hasPermission('ai:picture-sort:export')")
    @OperateLog(type = EXPORT)
    public void exportPictureSortExcel(@Valid PictureSortPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PictureSortDO> list = pictureSortService.getPictureSortPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "图片分类.xls", "数据", PictureSortRespVO.class,
                        BeanUtils.toBean(list, PictureSortRespVO.class));
    }

}